package org.wesc.boot.common.easyexcel;

import com.alibaba.excel.converters.Converter;
import com.alibaba.excel.enums.CellDataTypeEnum;
import com.alibaba.excel.metadata.CellData;
import com.alibaba.excel.metadata.GlobalConfiguration;
import com.alibaba.excel.metadata.property.ExcelContentProperty;

import java.text.NumberFormat;

/**
 * Double转String
 * @author Wesley
 * @date 2021/4/27
 */
public class DoubleToStringConverter implements Converter<Double> {

    @Override
    public Class<Double> supportJavaTypeKey() {
        return Double.class;
    }

    @Override
    public CellDataTypeEnum supportExcelTypeKey() {
        return CellDataTypeEnum.STRING;
    }

    @Override
    public Double convertToJavaData(CellData cellData, ExcelContentProperty contentProperty, GlobalConfiguration globalConfiguration) {
        return Double.parseDouble(cellData.getStringValue());
    }

    @Override
    public CellData<Double> convertToExcelData(Double value, ExcelContentProperty contentProperty, GlobalConfiguration globalConfiguration) {
        NumberFormat nf = NumberFormat.getInstance();
        nf.setGroupingUsed(false);
        return new CellData<>(nf.format(value));
    }
}
